//
//  UserListViewController.m
//  SELF 豆瓣
//
//  Created by dllo on 15/11/7.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserListViewController.h"
#import "UserListTableViewCell.h"
#import "LoigInViewController.h"
#import "myMovieViewController.h"
#import "DataBaseSingleton.h"
@interface UserListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *mytableView;
@property(nonatomic,retain)UINavigationController *login;
@end

@implementation UserListViewController

// 每次跳转到这个页面都会优先执行此方法，目的:判断当前的按钮是“登录” “还是注销”
-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"%@ ****************0为未登录,1为已经登录",[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"]);
    [super viewWillAppear:animated];
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    if ([string isEqualToString:@"1"]) {
        
        self.navigationItem.rightBarButtonItem.title = @"注销";
    }else if ([string isEqualToString:@"0"])
    {
        self.navigationItem.rightBarButtonItem.title = @"登录";
    }
    
    NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
    if (name.length == 0) {
        self.navigationItem.title = @"我的";
    }
    else
    {
        self.navigationItem.title = [NSString stringWithFormat:@"欢迎:%@",name];
    }
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //设置右侧按钮
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithTitle:@"33" style:UIBarButtonItemStylePlain target:self action:@selector(rigButton:)]autorelease];
    
    
    self.mytableView =[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    
    
    [self.view addSubview:self.mytableView];
    [_mytableView release];
    self.mytableView.delegate = self;
    self.mytableView.dataSource = self;
    
    [self.mytableView registerClass:[UserListTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}


//右侧登录按钮
-(void)rigButton:(UIBarButtonItem *)btn
{
    
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"isLogin"];
    if ([str isEqualToString:@"1"]) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要注销吗？" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            self.navigationItem.rightBarButtonItem.title = @"登录";
            [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"isLogin"];
            [[NSUserDefaults  standardUserDefaults]removeObjectForKey:@"name"];
            self.navigationItem.title = @"我的";
            
        }];
        
        [alertC addAction:action];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            
        }];
        [alertC addAction:action1];
        
    }
    
    
    
    else{
        LoigInViewController *loginVC = [[LoigInViewController alloc]init];
        self.login = [[UINavigationController alloc]initWithRootViewController:loginVC];
        
        self.login.navigationBar.backgroundColor = [UIColor colorWithRed:0.980 green:0.613 blue:0.948 alpha:1.000];
        
        
        [self.login setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentViewController:self.login animated:YES completion:^{
            
            
        }];
        
    }
    
}

//一个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
    
}


//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 12;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *user = @"cell";
    UserListTableViewCell   *cell = [tableView dequeueReusableCellWithIdentifier:user];
    if (indexPath.row == 0) {
        cell.nameLabel.text = @"我的活动";
    }
    if (indexPath.row == 1) {
        cell.nameLabel.text = @"我的电影";
        
    }
    if (indexPath.row == 2) {
        cell.nameLabel.text = @"清除缓存";
    }
    return cell;
    
    
    
}

//cell点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
   
    //点击我得活动
    if (indexPath.row == 0) {
        LoigInViewController *loginVC = [[LoigInViewController alloc]init];
        self.login = [[UINavigationController alloc]initWithRootViewController:loginVC];
        
        self.login.navigationBar.backgroundColor = [UIColor colorWithRed:0.980 green:0.613 blue:0.948 alpha:1.000];
        
        
        [self.login setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentViewController:self.login animated:YES completion:^{
            
            
        }];
        
    }
    
    
    
    //如果点击我的电影
    if (indexPath.row == 1)
    {
        if (name.length == 0)
        {
            LoigInViewController *loginVC = [[LoigInViewController alloc]init];
            self.login = [[UINavigationController alloc]initWithRootViewController:loginVC];
            
            self.login.navigationBar.backgroundColor = [UIColor colorWithRed:0.980 green:0.613 blue:0.948 alpha:1.000];
            
            
            [self.login setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            [self presentViewController:self.login animated:YES completion:^{
                
                
            }];
            
        }
        else
        {
            
            myMovieViewController *mymovieVC = [[myMovieViewController alloc]init];
            [self.navigationController pushViewController:mymovieVC animated:YES];
            
            
            
        }
        
    }
    
    //如果点击清除缓存 清除电影缓存
    
    
    if (indexPath.row == 2)
    {
        if (name.length == 0)
        {
            LoigInViewController *loginVC = [[LoigInViewController alloc]init];
            self.login = [[UINavigationController alloc]initWithRootViewController:loginVC];
            
            self.login.navigationBar.backgroundColor = [UIColor colorWithRed:0.980 green:0.613 blue:0.948 alpha:1.000];
            
            
            [self.login setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            [self presentViewController:self.login animated:YES completion:^{
                
                
            }];
            
        }
        else
        {
            
            [[DataBaseSingleton shareDataBase]openDB];
            [[DataBaseSingleton shareDataBase]dropMovieShouCangTbele];
            [[DataBaseSingleton shareDataBase]createShouCangTable];
            UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"清除缓存成功" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
            [actionsheet showInView:self.view.window];
            [actionsheet release];
            
            
        }
        
    }
    
    
    
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
